package com.mnnu.controller;

import com.mnnu.anno.OperateAnnotation;
import com.mnnu.pojo.Emp;
import com.mnnu.pojo.PageBean;
import com.mnnu.pojo.Result;
import com.mnnu.service.EmpService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.util.List;

@Slf4j
@RequestMapping("/emps")
@RestController
public class EmpController {

    @Autowired
    private EmpService empService;

    /**
     * 分页查询员工信息
     * @param name
     * @param gender
     * @param begin
     * @param end
     * @param page
     * @param pageSize
     * @return
     */
    @GetMapping
    public Result query(String name, Short gender,
                        @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate begin,
                        @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate end,
                        @RequestParam(defaultValue = "1") Integer page,
                        @RequestParam(defaultValue = "15") Integer pageSize) {
        log.info("分页查询员工信息");
        PageBean pageBean = empService.query(name, gender, begin, end, page, pageSize);
        return Result.success(pageBean);
    }

    /**
     * 批量删除员工信息
     * @param ids
     * @return
     */
    @OperateAnnotation
    @DeleteMapping("/{ids}")
    public Result delete(@PathVariable List<Integer> ids) {
        log.info("批量删除员工，员工id：{}", ids);
        empService.delete(ids);
        return Result.success();
    }

    /**
     * 添加员工信息
     * @param emp
     * @return
     */
    @OperateAnnotation
    @PostMapping
    public Result add(@RequestBody Emp emp) {
        log.info("添加员工信息：{}", emp);
        empService.add(emp);
        return Result.success();
    }

    /**
     * 根据id查询员工信息
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Result queryById(@PathVariable Integer id) {
        log.info("根据id查询回显员工信息");
        Emp emp = empService.queryById(id);
        return Result.success(emp);
    }

    /**
     * 修改员工信息
     * @param emp
     * @return
     */
    @OperateAnnotation
    @PutMapping
    public Result update(@RequestBody Emp emp) {
        log.info("修改员工信息");
        empService.update(emp);
        return Result.success();
    }
}
